package IU;

import Logica.Carrera;
import Logica.Fachada;
import Logica.Hipodromo;
import Logica.Jornada;
import Utilidades.ManejoFechas;
import java.util.Date;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Javier Arzarello
 */
public class PanelPrincipal extends javax.swing.JFrame {

    //SINGLETON
    private static PanelPrincipal instancia = null;
    
    public static PanelPrincipal getInstancia(){
        if(instancia==null){
            instancia = new PanelPrincipal();
        }
        return instancia;
    }
    
    //CONSTRUCTOR
    private PanelPrincipal(){    
        initComponents();
    }
    
    /**
     * Creates new form PanelPrincipal
     */
//    public PanelPrincipal() {
//        initComponents();
//    }
   
    // prueba de variable de hipódromo seleccionado
    private Hipodromo hipodromoSeleccionado = null;

    public Hipodromo getHipodromoSeleccionado() {
        return hipodromoSeleccionado;
    }

    public void setHipodromoSeleccionado(Hipodromo hipodromoSeleccionado) {
        this.hipodromoSeleccionado = hipodromoSeleccionado;
    }
    
    public Jornada getJornadaHoy(){
        return this.hipodromoSeleccionado.buscarJornada(new Date());
    }
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lblImagen = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        mnuArchivo = new javax.swing.JMenu();
        mnuCerrar = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        mnuSalir = new javax.swing.JMenuItem();
        mnuHipodromos = new javax.swing.JMenu();
        mnuSeleccionarHip = new javax.swing.JMenuItem();
        mnuCrearHip = new javax.swing.JMenuItem();
        mnuCarreras = new javax.swing.JMenu();
        mnuCrearCarrera = new javax.swing.JMenuItem();
        mnuAbrirCarrera = new javax.swing.JMenuItem();
        mnuCerrarCarrera = new javax.swing.JMenuItem();
        mnuGanadorCarrera = new javax.swing.JMenuItem();
        mnuMonitorearCarrera = new javax.swing.JMenuItem();
        mnuEmular = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lblImagen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/20111106175416.jpg"))); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblImagen)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblImagen)
                .addContainerGap())
        );

        mnuArchivo.setText("Archivo");

        mnuCerrar.setText("Cerrar Sesión");
        mnuCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuCerrarActionPerformed(evt);
            }
        });
        mnuArchivo.add(mnuCerrar);
        mnuArchivo.add(jSeparator1);

        mnuSalir.setText("Salir");
        mnuSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuSalirActionPerformed(evt);
            }
        });
        mnuArchivo.add(mnuSalir);

        jMenuBar1.add(mnuArchivo);

        mnuHipodromos.setText("Hipódromos");

        mnuSeleccionarHip.setText("Seleccionar");
        mnuSeleccionarHip.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuSeleccionarHipActionPerformed(evt);
            }
        });
        mnuHipodromos.add(mnuSeleccionarHip);

        mnuCrearHip.setText("Crear");
        mnuCrearHip.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuCrearHipActionPerformed(evt);
            }
        });
        mnuHipodromos.add(mnuCrearHip);

        jMenuBar1.add(mnuHipodromos);

        mnuCarreras.setText("Carreras");

        mnuCrearCarrera.setText("Crear");
        mnuCrearCarrera.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuCrearCarreraActionPerformed(evt);
            }
        });
        mnuCarreras.add(mnuCrearCarrera);

        mnuAbrirCarrera.setText("Abrir");
        mnuAbrirCarrera.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuAbrirCarreraActionPerformed(evt);
            }
        });
        mnuCarreras.add(mnuAbrirCarrera);

        mnuCerrarCarrera.setText("Cerrar");
        mnuCerrarCarrera.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuCerrarCarreraActionPerformed(evt);
            }
        });
        mnuCarreras.add(mnuCerrarCarrera);

        mnuGanadorCarrera.setText("Ganador");
        mnuGanadorCarrera.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuGanadorCarreraActionPerformed(evt);
            }
        });
        mnuCarreras.add(mnuGanadorCarrera);

        mnuMonitorearCarrera.setText("Monitorear");
        mnuMonitorearCarrera.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuMonitorearCarreraActionPerformed(evt);
            }
        });
        mnuCarreras.add(mnuMonitorearCarrera);

        jMenuBar1.add(mnuCarreras);

        mnuEmular.setText("Emular Terminal");
        mnuEmular.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnuEmularMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnuEmular);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(11, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mnuCrearHipActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuCrearHipActionPerformed

        new HipodromoCrearJD(this, true).setVisible(true);
    }//GEN-LAST:event_mnuCrearHipActionPerformed

    private void mnuCrearCarreraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuCrearCarreraActionPerformed
        // TODO add your handling code here:
        validarHipodromo();    
    }//GEN-LAST:event_mnuCrearCarreraActionPerformed

    private void mnuSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuSalirActionPerformed
        // TODO add your handling code here:
        System.exit(0); //CIERRA EL PROGRAMA
    }//GEN-LAST:event_mnuSalirActionPerformed

    private void mnuSeleccionarHipActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuSeleccionarHipActionPerformed
        seleccionarHipodromo();
    }//GEN-LAST:event_mnuSeleccionarHipActionPerformed

    private void mnuCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuCerrarActionPerformed
        // TODO add your handling code here:
        cerrarSesion();
    }//GEN-LAST:event_mnuCerrarActionPerformed

    private void mnuAbrirCarreraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuAbrirCarreraActionPerformed
        // TODO add your handling code here:
        abrirCarrera();
    }//GEN-LAST:event_mnuAbrirCarreraActionPerformed

    private void mnuCerrarCarreraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuCerrarCarreraActionPerformed
        // TODO add your handling code here:
        cerrarCarrera();
    }//GEN-LAST:event_mnuCerrarCarreraActionPerformed

    private void mnuGanadorCarreraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuGanadorCarreraActionPerformed
        // TODO add your handling code here:
        carreraGanador();
    }//GEN-LAST:event_mnuGanadorCarreraActionPerformed

    private void mnuEmularMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnuEmularMouseClicked
        // TODO add your handling code here:
        JOptionPane.showMessageDialog(this, "Se va a simular una terminal de apuestas");
        new LoginUsuario().setVisible(true);
    }//GEN-LAST:event_mnuEmularMouseClicked

    private void mnuMonitorearCarreraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuMonitorearCarreraActionPerformed
        // TODO add your handling code here:
        monitorearCarrera();
    }//GEN-LAST:event_mnuMonitorearCarreraActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PanelPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PanelPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PanelPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PanelPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PanelPrincipal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JLabel lblImagen;
    private javax.swing.JMenuItem mnuAbrirCarrera;
    private javax.swing.JMenu mnuArchivo;
    private javax.swing.JMenu mnuCarreras;
    private javax.swing.JMenuItem mnuCerrar;
    private javax.swing.JMenuItem mnuCerrarCarrera;
    private javax.swing.JMenuItem mnuCrearCarrera;
    private javax.swing.JMenuItem mnuCrearHip;
    private javax.swing.JMenu mnuEmular;
    private javax.swing.JMenuItem mnuGanadorCarrera;
    private javax.swing.JMenu mnuHipodromos;
    private javax.swing.JMenuItem mnuMonitorearCarrera;
    private javax.swing.JMenuItem mnuSalir;
    private javax.swing.JMenuItem mnuSeleccionarHip;
    // End of variables declaration//GEN-END:variables

    private void validarHipodromo() {
        if(hipodromoSeleccionado != null){
            CarreraCrearJD cc = new CarreraCrearJD(this, true);
            cc.setTitle("Crear Carrera - Hipódromo: " + hipodromoSeleccionado.getNombre());
            cc.setVisible(true);    
        }else
        {
            JOptionPane.showMessageDialog(null, "Debe seleccionar un Hipódromo"
                    + " antes de dar de alta una carrera.");
        }
    }

    private void cerrarSesion() {
        hipodromoSeleccionado = null;
        dispose();
        new LoginAdmin().setVisible(true);
    }

    private void abrirCarrera() {
        if(hipodromoSeleccionado != null){
            CarreraAbrir ca = new CarreraAbrir(this, true);
            ca.setTitle("Abrir Carrera - Hipódromo: " + hipodromoSeleccionado.getNombre());
            ca.setVisible(true);    
        }else
        {
            JOptionPane.showMessageDialog(null, "Debe seleccionar un Hipódromo"
                    + " antes de abrir una carrera.");
        }
    }

    private void cerrarCarrera() {
        if(hipodromoSeleccionado == null){
            JOptionPane.showMessageDialog(null, "Debe seleccionar un Hipódromo"
                    + " antes de cerrar una carrera.");
        }else{
            ManejoFechas mf = new ManejoFechas();
            Date fechaHoy = mf.formatoFecha(new Date());
            Jornada j = this.getHipodromoSeleccionado().buscarJornada(fechaHoy);
            Carrera c = j.buscarAbierta();
            
            if(c == null){
                JOptionPane.showMessageDialog(null, "No existen carreras abiertas para la jornada de hoy.");
            }else{
                CarreraCerrar cc = new CarreraCerrar(this, true);
                cc.setTitle("Cerrar Carrera - Hipódromo: " + hipodromoSeleccionado.getNombre());
                cc.setVisible(true);  
            }  
        }
    }

    private void carreraGanador() {
        if(hipodromoSeleccionado != null){
            CarreraGanador cg = new CarreraGanador(this, true);
            cg.setTitle("Elegir Ganador - Hipódromo: " + hipodromoSeleccionado.getNombre());
            cg.setVisible(true);    
        }else
        {
            JOptionPane.showMessageDialog(null, "Debe seleccionar un Hipódromo"
                    + " antes de elegir un ganador de una carrera.");
        }
    }

    private void seleccionarHipodromo() {
        
        Fachada f = Fachada.getInstancia();
        
        if(f.getHipodromos().isEmpty()){
            JOptionPane.showMessageDialog(null, "No hay hipódromos creados para seleccionar");
            new HipodromoCrearJD(this, true).setVisible(true);
        }else{
            new HipodromoSeleccionarJD(this, true).setVisible(true);
        }
    }

    private void monitorearCarrera() {
        
        if(hipodromoSeleccionado != null){
            if(hipodromoSeleccionado.getJornadas() == null){
                JOptionPane.showMessageDialog(null, "El hipódromo no cuenta con"
                    + " carreras para monitorear.");
            }else{
            CarreraMonitorear cm = new CarreraMonitorear(this, false);
            cm.setTitle("Monitorear - Hipódromo: " + hipodromoSeleccionado.getNombre());
            cm.setVisible(true);   
            }
        }else
        {
            JOptionPane.showMessageDialog(null, "Debe seleccionar un Hipódromo"
                    + " antes de poder monitorear carreras.");
        }
    }

}
